package com.mrdeer.iot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mrdeer.common.JSONResult;
import com.mrdeer.common.valid.Insert;
import com.mrdeer.common.valid.Update;
import com.mrdeer.iot.dto.DeviceEntityDTO;
import com.mrdeer.iot.entity.DeviceEntity;
import com.mrdeer.iot.service.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * 设备接口
 *
 * @Author: xie jianchu
 */
@Slf4j
@RestController
@RequestMapping("/mrdeer/device")
@Api(value = "设备管理接口", tags = "设备管理业务")
public class DeviceController {

    @Resource
    private DeviceService deviceService;

    @PostMapping("/addDevice")
    @ApiOperation(value = "新增设备信息")
    public JSONResult add(@Validated(Insert.class) @RequestBody DeviceEntity shopEntity) {
        try {
            deviceService.addDevice(shopEntity);
            return JSONResult.ok();
        } catch (Exception e) {
            return JSONResult.errorMsg(e.getMessage());
        }
    }

    @PostMapping("/deleteDevice/{id}")
    @ApiOperation(value = "根据ID删除设备")
    public JSONResult delete(@NotNull @PathVariable(value = "id") String id) {
        try {
            DeviceEntity deviceEntity = deviceService.selectDeviceEntityById(id);
            if (null != deviceEntity && deviceService.deleteDeviceEntityById(id)) {
                return JSONResult.ok("ok");
            }
        } catch (Exception e) {
            return JSONResult.errorMsg(e.getMessage());
        }
        return JSONResult.errorMsg("error");
    }

    @PostMapping("/updateDevice")
    @ApiOperation(value = "修改设备信息")
    public JSONResult update(@Validated(Update.class) @RequestBody DeviceEntity deviceEntity) {
        try {
            DeviceEntity update = deviceService.update(deviceEntity);
            if (null != update) {
                return JSONResult.ok(update);
            }
        } catch (Exception e) {
            return JSONResult.errorMsg(e.getMessage());
        }
        return JSONResult.errorMsg("error");
    }

    @GetMapping("/getDeviceById/{id}")
    @ApiOperation(value = "根据ID获取设备信息")
    public JSONResult selectById(@NotNull @PathVariable(value = "id") String id) {
        try {
            DeviceEntity deviceEntity = deviceService.selectDeviceEntityById(id);
            if (null != deviceEntity) {
                return JSONResult.ok(deviceEntity);
            }
        } catch (Exception e) {
            return JSONResult.ok(e.getMessage());
        }
        return JSONResult.errorMsg("error");
    }

    @PostMapping("/listDevice")
    @ApiOperation(value = "分页查询DeviceEntity")
    public JSONResult list(@RequestBody(required = false) DeviceEntityDTO dto) {
        try {
            Page<DeviceEntity> page = deviceService.deviceList(dto);
            if (null != page) {
                return JSONResult.ok(page);
            }
        } catch (Exception e) {
            return JSONResult.errorMsg(e.getMessage());
        }
        return JSONResult.errorMsg("error");
    }
}
